package cn.diancan.Tags.utils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.*;

import cn.diancan.Tags.abs.BaseDirective;


public class PaginationDirective extends BaseDirective {

	private static final String PATTERN = "pattern";
	private static final String PAGENUM = "pageNumber";
	private static final String TOTALPAGES = "totalPages";
	private static final String SEGMENTCOUNT = "segmentCount";
	private static final String HASPREVIOUS = "hasPrevious";
	private static final String HASNEXT = "hasNext";
	private static final String ISFIRST = "isFirst";
	private static final String ISLAST = "isLast";
	private static final String PREVIOUSPAGENUMBER = "previousPageNumber";
	private static final String NEXTPAGENUMBER = "nextPageNumber";
	private static final String FIRSTPAGENUMBER = "firstPageNumber";
	private static final String LASTPAGENUMBER = "lastPageNumber";
	private static final String SEGMENT = "segment";

	@SuppressWarnings("rawtypes")
	public void execute(Environment env, Map params, 
			TemplateModel loopVars[], TemplateDirectiveBody body) throws TemplateException, IOException{
		String pattern = (String)FrontUtils.getParameter(PATTERN, String.class, params);
		Integer pageNumber = (Integer)FrontUtils.getParameter(PAGENUM, Integer.class, params);
		Integer totalPages = (Integer)FrontUtils.getParameter(TOTALPAGES, Integer.class, params);
		Integer segmentCount = (Integer)FrontUtils.getParameter(SEGMENTCOUNT, Integer.class, params);
		if (pageNumber == null || pageNumber < 1) {
			pageNumber = 1;
		}
		if (totalPages == null || totalPages < 1){
			totalPages = 1;
		}
		if (segmentCount == null || segmentCount < 1){
			segmentCount = 5;
		}
		boolean hasPrevious = pageNumber > 1;
		boolean hasNext = pageNumber < totalPages;
		boolean isFirst = pageNumber == 1;
		boolean isLast = pageNumber.equals(totalPages) ;
		int previousPageNumber = pageNumber - 1;
		int nextPageNumber = pageNumber + 1;
		int firstPageNumber = 1;
		int lastPageNumber = totalPages;
		int i1 = pageNumber - (int)Math.floor((double)(segmentCount - 1) / 2);
		int j1 = pageNumber.intValue() + (int)Math.ceil((double)(segmentCount - 1) / 2);
		if (i1 < 1) {
			i1 = 1;
		}
		if (j1 > totalPages){
			j1 = totalPages;
		}
		List<Integer> segment = new ArrayList<Integer>();
		for (int k1 = i1; k1 <= j1; k1++){
			segment.add(k1);
		}
		HashMap<String, Object> hashmap = new HashMap<String, Object>();
		hashmap.put(PATTERN, pattern);
		hashmap.put(PAGENUM, pageNumber);
		hashmap.put(TOTALPAGES, totalPages);
		hashmap.put(SEGMENTCOUNT, segmentCount);
		hashmap.put(HASPREVIOUS, hasPrevious);
		hashmap.put(HASNEXT, hasNext);
		hashmap.put(ISFIRST, isFirst);
		hashmap.put(ISLAST, isLast);
		hashmap.put(PREVIOUSPAGENUMBER, previousPageNumber);
		hashmap.put(NEXTPAGENUMBER, nextPageNumber);
		hashmap.put(FIRSTPAGENUMBER, firstPageNumber);
		hashmap.put(LASTPAGENUMBER, lastPageNumber);
		hashmap.put(SEGMENT, segment);
		
		renderParamToBody(hashmap, env, body);
	}
}
